Option Strict On
Option Explicit On

'Copyright (c) 2007-2010, William Severance, Jr., WESNet Designs
'All rights reserved.

'Redistribution and use in source and binary forms, with or without modification, are permitted
'provided that the following conditions are met:

'Redistributions of source code must retain the above copyright notice, this list of conditions
'and the following disclaimer.

'Redistributions in binary form must reproduce the above copyright notice, this list of conditions
'and the following disclaimer in the documentation and/or other materials provided with the distribution.

'Neither the name of William Severance, Jr. or WESNet Designs may be used to endorse or promote
'products derived from this software without specific prior written permission.

'Disclaimer: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
'            OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
'            AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE
'            FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
'            LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
'            INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
'            OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
'            IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'Although I will try to answer questions regarding the installation and use of this software when
'such questions are submitted via e-mail to the below address, no promise of further
'support or enhancement is made nor should be assumed.

'Developer Contact Information:
'     William Severance, Jr.
'     WESNet Designs
'     679 Upper Ridge Road
'     Bridgton, ME 04009
'     Phone: 207-647-9375
'     E-Mail: bill@wesnetdesigns.com
'     Website: www.wesnetdesigns.com

Imports DotNetNuke.Services.Exceptions

Namespace WESNet.DNN.Modules.EPrayer
    Partial Class ManageModerators
        Inherits EPrayerModuleBase

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try
                If Not IsAdministrator Then Response.Redirect(AccessDeniedURL(), True)
                cmdExpiresCalendar.NavigateUrl = DotNetNuke.Common.Utilities.Calendar.InvokePopupCal(tbExpiresDate)
                If Not Page.IsPostBack Then
                    Utilities.LocalizeGridView(gvModerators, LocalResourceFile)
                    BindData()
                    FillAvailableModeratorsDDL()
                End If
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try

        End Sub

        Private Sub BindData()
            Dim epc As New EPrayerController
            gvModerators.DataSource = epc.GetModerators(ModuleId, False)
            gvModerators.DataBind()
        End Sub

        Private Sub FillAvailableModeratorsDDL()
            Dim arrAvailable As New ArrayList
            Dim mpc As New DotNetNuke.Security.Permissions.ModulePermissionController
            Dim rc As New DotNetNuke.Security.Roles.RoleController
            Dim mp As DotNetNuke.Security.Permissions.ModulePermissionInfo
            For Each mp In DotNetNuke.Security.Permissions.ModulePermissionController.GetModulePermissions(ModuleId, TabId) ' mpc.GetModulePermissionsCollectionByModuleID(ModuleId, TabId)
                If mp.PermissionCode = Permission.Code And mp.PermissionKey = Permission.ModerateKey Then
                    Dim RoleName As String = mp.RoleName
                    If IsDBNull(mp.RoleName) Then
                        'If mp.RoleName is null then must be individual permission so
                        Dim li As New ListItem(mp.Username, mp.UserID.ToString)
                        If Not arrAvailable.Contains(li) Then arrAvailable.Add(li)
                    Else
                        For Each ui As Entities.Users.UserInfo In rc.GetUsersByRoleName(PortalId, RoleName)
                            Dim li As New ListItem(ui.DisplayName, ui.UserID.ToString)
                            If Not arrAvailable.Contains(li) Then arrAvailable.Add(li)
                        Next
                    End If

                End If
            Next
            Dim i As Integer = arrAvailable.Count - 1
            If gvModerators.DataKeys.Count > 0 Then
                Do While i >= 0
                    Dim li As ListItem = CType(arrAvailable.Item(i), ListItem)
                    Dim j As Integer = 0
                    Do While j < gvModerators.DataKeys.Count
                        Dim dk As DataKey = gvModerators.DataKeys(j)
                        If li.Value = dk.Value.ToString Then
                            arrAvailable.Remove(li)
                            Exit Do
                        End If
                        j += 1
                    Loop
                    i -= 1
                Loop
            End If
            With ddlAvailableModerators
                .DataSource = arrAvailable
                .DataBind()
                .Items.Insert(0, New ListItem(Localize("Select_New_Moderator"), "0"))
            End With
            cmdAddModerator.Enabled = arrAvailable.Count > 0
        End Sub


        Protected Sub cmdReturn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdReturn.Click
            Try
                Response.Redirect(ReturnUrl, True)
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Protected Sub cmdAddModerator_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdAddModerator.Click
            If ddlAvailableModerators.SelectedIndex > 0 Then
                Dim ExpiresDate As DateTime
                If Not String.IsNullOrEmpty(tbExpiresDate.Text) Then
                    ExpiresDate = DateTime.Parse(tbExpiresDate.Text)
                End If
                Dim epc As New EPrayerController
                epc.AddModerator(ModuleId, CType(ddlAvailableModerators.SelectedValue, Integer), ExpiresDate)
                BindData()
                FillAvailableModeratorsDDL()
            End If
        End Sub

        Protected Sub gvModerators_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvModerators.RowCancelingEdit
            gvModerators.EditIndex = -1
            BindData()
        End Sub

        Protected Sub gvModerators_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvModerators.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim mi As ModeratorInfo = CType(e.Row.DataItem, ModeratorInfo)

                If e.Row.RowIndex = gvModerators.EditIndex Then
                    Dim tbExpires As TextBox = CType(e.Row.Cells(4).FindControl("tbExpires"), TextBox)
                    If Null.IsNull(mi.ExpiresDate) Then
                        tbExpires.Text = String.Empty
                    End If
                    CType(e.Row.Cells(4).FindControl("cmdExpiresCalendar"), HyperLink).NavigateUrl = DotNetNuke.Common.Utilities.Calendar.InvokePopupCal(tbExpires)
                Else
                    If Null.IsNull(mi.ExpiresDate) Then
                        CType(e.Row.Cells(4).FindControl("lblExpires"), System.Web.UI.WebControls.Label).Text = String.Empty
                    End If

                End If

                'If Null.IsNull(mi.ExpiresDate) Then
                '    If e.Row.RowIndex = gvModerators.EditIndex Then
                '        CType(e.Row.Cells(4).Controls(0), TextBox).Text = String.Empty
                '    Else
                '        e.Row.Cells(4).Text = String.Empty
                '    End If
                'End If

                If Null.IsNull(mi.LastModeratedDate) Then
                    e.Row.Cells(6).Text = String.Empty
                End If
            End If
        End Sub

        Protected Sub gvModerators_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvModerators.RowDeleting
            Dim UserID As Integer = CInt(gvModerators.DataKeys(e.RowIndex).Value)
            Dim epc As New EPrayerController
            epc.DeleteModerator(ModuleId, UserID)
            BindData()
            FillAvailableModeratorsDDL()
        End Sub

        Protected Sub gvModerators_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvModerators.RowEditing
            gvModerators.EditIndex = e.NewEditIndex
            BindData()
        End Sub

        Protected Sub gvModerators_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvModerators.RowUpdating
            Try
                Dim ExpiresDate As DateTime
                Dim IsSuspended As Boolean
                Dim UserID As Integer = CType(gvModerators.DataKeys(e.RowIndex).Value, Integer)
                Dim UpdatingRow As GridViewRow = gvModerators.Rows(e.RowIndex)

                'Dim tbExpires As TextBox = CType(UpdatingRow.Cells(4).Controls(0), TextBox)

                Dim tbExpires As TextBox = CType(UpdatingRow.Cells(4).FindControl("tbExpires"), TextBox)
                Dim cbIsSuspended As CheckBox = CType(UpdatingRow.Cells(7).Controls(0), CheckBox)

                If Not String.IsNullOrEmpty(tbExpires.Text) Then
                    ExpiresDate = DateTime.Parse(tbExpires.Text)
                Else
                    ExpiresDate = Null.NullDate
                End If
                IsSuspended = cbIsSuspended.Checked
                Dim epc As New EPrayerController
                epc.UpdateModerator(ModuleId, UserID, ExpiresDate, IsSuspended)
                gvModerators.EditIndex = -1
                BindData()
                FillAvailableModeratorsDDL()
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try

        End Sub


        Protected Sub gvModerators_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvModerators.SelectedIndexChanged
            Dim UserID As Integer = CInt(gvModerators.SelectedValue)
            Response.Redirect(EditUrl("UserID", UserID.ToString, "MngUsers", "returnurl=" & Request.RawUrl), True)
        End Sub
    End Class
End Namespace
